package tk.xboot.kafka.kfk;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFuture;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Component
public class Sender {

    @Resource
    private KafkaTemplate<String, String> kafkaTemplate;

    private int key;

    @Value("${ws.kfk.topic}")
    private String topic;

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    @Scheduled(fixedRate = 1000)
    private void reportCurrentTime() {
        String dt = dateFormat.format(new Date());
        log.info(">>> fixed rate: The time is now {} counts-store", dt);
        String msg = "Msg: 0_0 " + dt;
        ListenableFuture<SendResult<String, String>> result = kafkaTemplate.send(topic, ++key + "", msg);
    }
}